//////////Data Preparing
import delimited "/Users/shakali/Dropbox/submission_version/main_replication_data.csv"
set more off
encode iso3, gen(iso3_numeric)
xtset iso3_numeric year 
tab year if iso3_numeric!=.

//////////Generating variables we need
///Log value of natural resource rents
gen natural_resource_rents_1 = natural_resource_rents +1
gen ln_natural_resources = log(natural_resource_rents_1)
drop natural_resource_rents_1
/// Natural resource rents lagged by 1 year (t-1)
by iso3_numeric: gen lag_natural_resource_rents = natural_resource_rents[_n-1]
///Log value of tax revenue
gen tax_revenue_1 = tax_revenue +1
gen ln_tax_revenue = log(tax_revenue_1)
drop tax_revenue_1
///Log value of trade openness
gen trade_openness_1 = trade_openness +1
gen ln_trade_openness = log(trade_openness_1)
drop trade_openness_1
///Log value of corruption control
gen corruption_control_1 = corruption_control +1
gen ln_corruption_control = log(corruption_control_1)
drop corruption_control_1
///Reverse scales of variables
gen repression_reverse_t1 = -v2csreprss_t1
/// Label variables
label var repression_reverse_t1 "Repression"
label var natural_resource_rents_t1 "Natural resource rents"

//////////Descriptive statistics
summarize ln_chinese_fdi_flows_t1 natural_resource_rents_t1 ln_natural_resources ///
    repression_reverse_t1 political_stability political_stability_t1 ///
    ln_corruption_control ln_tax_revenue ln_trade_openness ///
    vdem_polyarchy ln_gdp_2015 pwt_pop

////////// Table 1. Regression Estimates of the Effect of Political Repression on Chinese FDI Inflows

eststo clear
eststo: quietly reg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1 if sea == 1, vce(r)
eststo: quietly reg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1 political_stability_t1 ln_natural_resources ln_tax_revenue ln_corruption_control ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop if sea == 1, vce(r)
eststo: quietly xtreg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1 political_stability_t1 ln_natural_resources ln_tax_revenue ln_corruption_control ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1, fe vce(r)

esttab using repression_baseline.htm, ///
    indicate("Country Fixed effects = *.countryx" "Year Fixed effects = *.year") ///
    mtitles("OLS (Repression Only)" ///
            "OLS (with Controls)" ///
            "FE (with Controls)") ///
    coeflabels( ///
        repression_reverse_t1 "Repression (t-1)" ///
        political_stability_t1 "Political Stability (t-1)" ///
        ln_natural_resources "Natural Resources (logged)" ///
        ln_tax_revenue "Tax Revenue (logged)" ///
        ln_corruption_control "Corruption Control (logged)" ///
        ln_trade_openness "Trade Openness (logged)" ///
        vdem_polyarchy "Democracy" ///
        ln_gdp_2015 "GDP per capita (logged)" ///
        pwt_pop "Population (logged)" ///
    ) ///
    order( ///
        repression_reverse_t1 ///
        political_stability_t1 ///
        ln_natural_resources ///
        ln_tax_revenue ///
        ln_corruption_control ///
        ln_trade_openness ///
        vdem_polyarchy ///
        ln_gdp_2015 ///
        pwt_pop ///
    ) ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    stats(r2 r2_a N, ///
    labels("R<sup>2</sup>" "Adj. R<sup>2</sup>" "Observations")) //////
    addnote("Robust standard errors are reported." ///
            "All control variables are logged where appropriate.") ///
    b se replace


////////// Table 2. Interaction Effects of Repression with Political Stability and Resource Rents on Chinese FDI Inflows

eststo clear
eststo: quietly xtreg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1##c.political_stability_t1 ln_natural_resources ln_tax_revenue ln_corruption_control ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1, fe vce(r)
eststo: quietly xtreg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1##c.political_stability_t1 ln_natural_resources ln_tax_revenue ln_corruption_control ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop if sea == 1, re vce(r)  
eststo: quietly xtreg c.ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 political_stability ln_corruption_control ln_tax_revenue ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1, fe vce(r)
eststo: quietly xtreg c.ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 political_stability ln_corruption_control ln_tax_revenue ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop if sea == 1, re vce(r) 

esttab using Table2.htm, ///
    indicate("Country Fixed effects = *.countryx" "Year Fixed effects = *.year") ///
    mtitles("Repression × Stability (FE)" ///
            "Repression × Stability (RE)" ///
            "Repression × Resource Rents (FE)" ///
            "Repression × Resource Rents (RE)") ///
    coeflabels( ///
        repression_reverse_t1 "Repression (t-1)" ///
        political_stability_t1 "Political Stability (t-1)" ///
        c.repression_reverse_t1#c.political_stability_t1 "Repression × Political Stability (t-1)" ///
        natural_resource_rents_t1 "Natural Resource Rents (t-1)" ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 "Repression × Resource Rents (t-1)" ///
        political_stability "Political Stability" ///
        ln_tax_revenue "Tax Revenue (logged)" ///
        ln_corruption_control "Corruption Control (logged)" ///
        ln_trade_openness "Trade Openness (logged)" ///
        vdem_polyarchy "Democracy" ///
        ln_gdp_2015 "GDP per capita (logged)" ///
        pwt_pop "Population (logged)" ///
        ln_natural_resources "Natural Resources (logged)" ///
    ) ///
    order( ///
        repression_reverse_t1 ///
        political_stability_t1 ///
        c.repression_reverse_t1#c.political_stability_t1 ///
        natural_resource_rents_t1 ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 ///
        political_stability ///
        ln_tax_revenue ///
        ln_corruption_control ///
        ln_trade_openness ///
        vdem_polyarchy ///
        ln_gdp_2015 ///
        pwt_pop ///
        ln_natural_resources ///
    ) ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    addnote("All control variables are logged where appropriate." ///
            "Robust standard errors are reported.") ///
    b se replace

////////// Table A.1: Fixed Effects Regression Including Both Interaction Terms.

eststo clear
eststo: quietly xtreg c.ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 c.repression_reverse_t1##c.political_stability_t1 ln_corruption_control ln_tax_revenue ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop if sea == 1, fe vce(r) 


esttab using TableA1.htm, ///
    mtitles("FE: Both Interactions") ///
    coeflabels( ///
        repression_reverse_t1 "Repression (t-1)" ///
        political_stability_t1 "Political Stability (t-1)" ///
        c.repression_reverse_t1#c.political_stability_t1 "Repression × Political Stability (t-1)" ///
        natural_resource_rents_t1 "Natural Resource Rents (t-1)" ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 "Repression × Resource Rents (t-1)" ///
        political_stability "Political Stability" ///
        ln_tax_revenue "Tax Revenue (logged)" ///
        ln_corruption_control "Corruption Control (logged)" ///
        ln_trade_openness "Trade Openness (logged)" ///
        vdem_polyarchy "Democracy" ///
        ln_gdp_2015 "GDP per capita (logged)" ///
        pwt_pop "Population (logged)" ///
    ) ///
    order( ///
        repression_reverse_t1 ///
        political_stability_t1 ///
        c.repression_reverse_t1#c.political_stability_t1 ///
        natural_resource_rents_t1 ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 ///
        political_stability ///
        ln_tax_revenue ///
        ln_corruption_control ///
        ln_trade_openness ///
        vdem_polyarchy ///
        ln_gdp_2015 ///
        pwt_pop ///
    ) ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    addnote("Appendix model including both interaction terms." ///
            "All control variables are logged where appropriate." ///
            "Robust standard errors are reported.") ///
    b se replace

////////// Table A.2: Robustness Check: Removing Outliers Based on Studentized Residuals

////Create r-student residuals
reg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1##c.political_stability_t1 ln_natural_resources vdem_polyarchy corruption trade_openness ln_gdp_2015 ln_tax_revenue ln_population i.year i.countryx if sea == 1
predict rstudent, rstudent

reg c.ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 ///
	political_stability ln_corruption_control ln_tax_revenue ln_trade_openness ///
	vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1
predict rstudent2, rstudent

eststo clear
eststo: quietly xtreg ln_chinese_fdi_flows_t1 c.repression_reverse_t1##c.political_stability_t1 ///
    ln_natural_resources ln_tax_revenue ln_corruption_control ln_trade_openness ///
    vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1, fe vce(r)

eststo: quietly xtreg ln_chinese_fdi_flows_t1 c.repression_reverse_t1##c.political_stability_t1 ///
    ln_natural_resources ln_tax_revenue ln_corruption_control ln_trade_openness ///
    vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1 & abs(rstudent) < 3, fe vce(r)

eststo: quietly xtreg c.ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 ///
	political_stability ln_corruption_control ln_tax_revenue ln_trade_openness ///
	vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1, fe vce(r)
	
eststo: quietly xtreg ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 ///
    political_stability ln_tax_revenue ln_corruption_control ln_trade_openness ///
    vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1 & abs(rstudent2) < 3, fe vce(r)


esttab using TableA2.htm, ///
    label ///
    se ///
    b(3) se(3) ///
    star(* 0.1 ** 0.05 *** 0.01) ///
    drop(*.year *.countryx) ///
    mtitles("Stability (Full)" "Stability (No Outliers)" ///
            "Resources (Full)" "Resources (No Outliers)") ///
    coeflabels( ///
        repression_reverse_t1 "Repression (t-1)" ///
        political_stability_t1 "Political Stability (t-1)" ///
        c.repression_reverse_t1#c.political_stability_t1 ///
            "Repression × Political Stability (t-1)" ///
        natural_resource_rents_t1 "Natural Resource Rents (t-1)" ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 ///
            "Repression × Resource Rents (t-1)" ///
        political_stability "Political Stability" ///
        ln_tax_revenue "Tax Revenue (logged)" ///
        ln_corruption_control "Corruption Control (logged)" ///
        ln_trade_openness "Trade Openness (logged)" ///
        vdem_polyarchy "Democracy" ///
        ln_gdp_2015 "GDP per capita (logged)" ///
        pwt_pop "Population (logged)" ///
        ln_natural_resources "Natural Resources (logged)" ///
    ) ///
    order( ///
        repression_reverse_t1 ///
        c.repression_reverse_t1#c.political_stability_t1 ///
        natural_resource_rents_t1 ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 ///
        political_stability ///
        ln_tax_revenue ///
        ln_corruption_control ///
        ln_trade_openness ///
        vdem_polyarchy ///
        ln_gdp_2015 ///
        pwt_pop ///
        ln_natural_resources ///
    ) ///
    stats(r2 r2_a N, ///
    labels("R<sup>2</sup>" "Adj. R<sup>2</sup>" "Observations") ///
    fmt(2 2 0)) ///
    title("Robustness Check: Removing Outliers Based on Studentized Residuals") ///
    replace
	
	
//////////Figure 1. Regression Coefficients from Table A.1.

eststo both_interaction: ///
xtreg c.ln_chinese_fdi_flows_t1 ///
    c.natural_resource_rents_t1##c.repression_reverse_t1 ///
    c.repression_reverse_t1##c.political_stability_t1 ///
    political_stability ln_corruption_control ln_tax_revenue ///
    ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop ///
    if sea == 1, fe vce(r)

coefplot both_interaction, ///
    keep( ///
        repression_reverse_t1 ///
        political_stability_t1 ///
        natural_resource_rents_t1 ///
        c.repression_reverse_t1#c.political_stability_t1 ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 ///
    ) ///
    rename( ///
        repression_reverse_t1 = "Repression (t-1)" ///
        political_stability_t1 = "Political Stability (t-1)" ///
        natural_resource_rents_t1 = "Natural Resource Rents (t-1)" ///
        c.repression_reverse_t1#c.political_stability_t1 = "Repression × Political Stability" ///
        c.natural_resource_rents_t1#c.repression_reverse_t1 = "Repression × Resource Rents" ///
    ) ///
    drop(_cons) ///
    xline(0, lpattern(dash)) ///
    title("") ///
    ylabel(, angle(horizontal)) ///
    legend(off) ///
    mlabposition(12) ///
    msymbol(circle) ///
    ciopts(recast(rcap))

//////////Figure 2. Marginal effects of state repression on Chinese FDI inflows at varying levels of political stability. Error bars represent 95% confidence intervals.

xtreg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1##c.political_stability_t1 ln_natural_resources ln_tax_revenue ln_corruption_control ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1, fe vce(r)
margins, at(repression_reverse_t1=(-3 3) political_stability_t1=(0(20)100)) vsquish level(90)
margins, dydx(repression_reverse_t1) at(political_stability_t1=(0(20)100)) vsquish level(90)
marginsplot, yline(0) title("") xtitle("Political Stability (t-1)")
graph export "marginal_political.png", width(2000) replace

//////////Figure 3. Marginal effects of state repression on Chinese FDI inflows at varying levels of natural resource rents. Error bars represent 95% confidence intervals.

xtreg c.ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 political_stability ln_corruption_control ln_tax_revenue ln_trade_openness vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1, fe vce(r)
margins, at(repression_reverse_t1=(-3(1)3) natural_resource_rents_t1=(0(50)87))
marginsplot, xdimension(repression_reverse_t1) by(natural_resource_rents_t1)
margins, at(repression_reverse_t1=(-3 3) natural_resource_rents_t1=(0(20)87)) vsquish
margins, dydx(repression_reverse_t1) at(natural_resource_rents_t1=(0(20)87)) vsquish
marginsplot, yline(0) title("") xtitle("Natural Resource Rents (t-1)")
graph export "marginal_natural.png", width(2000) replace

//////////Figure A.1. Outlier Diagnostics for Model 1 (in Table 2): Studentized Residuals. 

reg c.ln_chinese_fdi_flows_t1 c.repression_reverse_t1##c.political_stability_t1 ln_natural_resources vdem_polyarchy corruption trade_openness ln_gdp_2015 ln_tax_revenue ln_population i.year i.countryx if sea == 1
predict rstudent, rstudent
gen outlier = abs(rstudent) > 3
list countryx year ln_chinese_fdi_flows_t1 rstudent if outlier == 1
gen obs_id = _n
scatter rstudent obs_id if sea == 1, mlabel(country_namex)
	

gen outlier_label = ""
replace outlier_label = country_namex if abs(rstudent) > 3
	
	
scatter rstudent obs_id if sea == 1, ///
    mlabel(outlier_label) ///
    msymbol(circle) ///
    mcolor(blue) ///
    yline(-3 3, lcolor(red) lpattern(dash)) ///
    ylabel(-4(1)4) ///
    xtitle("Observation ID") ///
    ytitle("Studentized Residual") ///
    graphregion(margin(l+5 r+5 t+5 b+5))
	
graph export "studentized_residuals_plot_A.png", width(2000) replace

//////////Figure A.2. Outlier Diagnostics for Model 3 (in Table 2): Studentized Residuals.
reg c.ln_chinese_fdi_flows_t1 c.natural_resource_rents_t1##c.repression_reverse_t1 ///
	political_stability ln_corruption_control ln_tax_revenue ln_trade_openness ///
	vdem_polyarchy ln_gdp_2015 pwt_pop i.year i.countryx if sea == 1
predict rstudent2, rstudent
gen outlier2 = abs(rstudent2) > 3
list countryx year ln_chinese_fdi_flows_t1 rstudent2 if outlier2 == 1
gen obs_id_2 = _n
scatter rstudent2 obs_id_2 if sea == 1, mlabel(country_namex)

gen outlier_label_2 = ""
replace outlier_label_2 = country_namex if abs(rstudent2) > 3
	
	
scatter rstudent2 obs_id_2 if sea == 1, ///
    mlabel(outlier_label_2) ///
    msymbol(circle) ///
    mcolor(blue) ///
    yline(-3 3, lcolor(red) lpattern(dash)) ///
    ylabel(-4(1)4) ///
    xtitle("Observation ID") ///
    ytitle("Studentized Residual") ///
    graphregion(margin(l+5 r+5 t+5 b+5))
	
graph export "studentized_residuals_plot_B.png", width(2000) replace

///Check missing
//list ln_chinese_fdi_flows_t1 country year if missing(ln_chinese_fdi_flows_t1) & sea == 1
///Check non-missing
//list ln_chinese_fdi_flows_t1 country year if !missing(ln_chinese_fdi_flows_t1) & sea == 1
//twoway (scatter ln_chinese_fdi_flows_t1 year if sea == 1, ///
    mlabel(country) mlabposition(0) msymbol(O)), ///
    xtitle("Year") ///
    ytitle("Chinese FDI (t-1)") ///
    title("Scatterplot: FDI vs. Political Stability with Country Labels")
